/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.profile_manager.profile;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Personal extends Profile {
	public String headline, firstThing, turnOffs, firstDate, pastRelation,
			fiveThi, bedroom, idealMatch;

	public int heigth;

	public int eyeColor, hairColor, looks, bestFeat,build;

	public String toXML() {
		StringBuilder output = new StringBuilder();
		output
				.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources>");
		output.append("<item name=\"bestFeat\">" + bestFeat + "</item>");
		output.append("<item name=\"looks\">" + looks + "</item>");
		output.append("<item name=\"build\">" + build + "</item>");
		output.append("<item name=\"hairColor\">" + hairColor + "</item>");
		output.append("<item name=\"eyeColor\">" + eyeColor + "</item>");
		output.append("<item name=\"heigth\">" + heigth + "</item>");
		output.append("<item name=\"idealMatch\">" + idealMatch + "</item>");
		output.append("<item name=\"bedroom\">" + bedroom + "</item>");
		output.append("<item name=\"fiveThi\">" + fiveThi + "</item>");
		output.append("<item name=\"pastRelation\">" + pastRelation
				+ "</item>");
		output.append("<item name=\"firstDate\">" + firstDate + "</item>");
		output.append("<item name=\"turnOffs\">" + turnOffs + "</item>");
		output.append("<item name=\"firstThing\">" + firstThing + "</item>");
		output.append("<item name=\"headline\">" + headline + "</item>");
		output.append("</resources>");
		
	/*	StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i< output.length(); i++){
			char c;
			if((c = output.charAt(i))==' '){
				sb.append("%20");
			}else{
				sb.append(c);
			}
		}*/
		return output.toString();
	}

	public void fromXML(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	class MyHandler extends DefaultHandler {
		String lastElement;

		boolean begin;

		public void endElement(String uri, String localName, String qName) {
			begin = false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin = true;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
			}
		}

		public void characters(char[] ch, int start, int length) {
			String s = new String(ch, start, length);
			if(s.compareTo("null")==0)return;
			if (lastElement == null)
				return;
			if (!begin)
				return;
			if (lastElement.compareTo("heigth") == 0) {
				Personal.this.heigth = Integer.parseInt(s);
			} else if (lastElement.compareTo("eyeColor") == 0) {
				Personal.this.eyeColor = Integer.parseInt(s);
			} else if (lastElement.compareTo("hairColor") == 0) {
				Personal.this.hairColor = Integer.parseInt(s);
			} else if (lastElement.compareTo("build") == 0) {
				Personal.this.build = Integer.parseInt(s);
			} else if (lastElement.compareTo("looks") == 0) {
				Personal.this.looks = Integer.parseInt(s);
			} else if (lastElement.compareTo("bestFeat") == 0) {
				Personal.this.bestFeat = Integer.parseInt(s);
			} else if (lastElement.compareTo("headline") == 0) {
				Personal.this.headline = s;
			} else if (lastElement.compareTo("firstThing") == 0) {
				Personal.this.firstThing = s;
			} else if (lastElement.compareTo("turnOffs") == 0) {
				Personal.this.turnOffs = s;
			} else if (lastElement.compareTo("firstDate") == 0) {
				Personal.this.firstDate = s;
			} else if (lastElement.compareTo("pastRelation") == 0) {
				Personal.this.pastRelation = s;
			} else if (lastElement.compareTo("fiveThi") == 0) {
				Personal.this.fiveThi = s;
			} else if (lastElement.compareTo("bedroom") == 0) {
				Personal.this.bedroom = s;
			} else if (lastElement.compareTo("idealMatch") == 0) {
				Personal.this.idealMatch = s;
			}

		}

	}
}
